public class Solution44 {
    public boolean isMatch(String s, String p) {
        boolean[][] dp = new boolean[p.length() + 1][s.length() + 1];
        dp[0][0] = true;
        for (int i = 0; i < p.length(); i++) {
            if (p.charAt(i) == '*') {
                dp[i + 1][0] = dp[i][0];
            } else {
                break;
            }
        }
        for (int i = 1; i < p.length() + 1; i++) {
            for (int j = 1; j < s.length() + 1; j++) {
                if (p.charAt(i - 1) == '*') {
                    dp[i][j] = dp[i - 1][j] || dp[i][j - 1];
                } else {
                    dp[i][j] = dp[i - 1][j - 1] && (p.charAt(i - 1) == '?' || p.charAt(i - 1) == s.charAt(j - 1));
                }
            }
        }
        return dp[p.length()][s.length()];
    }

/*    private boolean match(String p, String s, int i, int j) {
        if (j == 0) {
            return false;
        }
        if (p.charAt(i - 1) == '?') {
            return true;
        }
        return p.charAt(i - 1) == s.charAt(j - 1);
    }*/
}
